package com.vpx.server.service.system.impl;

import cn.hutool.core.util.XmlUtil;
import cn.hutool.json.JSONObject;
import com.vpx.server.bean.enums.ProjectConfigTypeEnum;
import com.vpx.server.service.system.SqlExtService;
import com.vpx.server.service.system.XmlShareConfigService;
import org.springframework.stereotype.Service;
import org.w3c.dom.Document;
import org.w3c.dom.Element;

import javax.annotation.Resource;
import java.io.File;
import java.util.List;
import java.util.stream.IntStream;

@Service
public class XmlShareConfigServiceImpl implements XmlShareConfigService {
    @Resource
    SqlExtService sqlExtService;
    @Override
    public void createShareConfigXml(JSONObject project) {
        String projectName = project.getStr("project_name");
        String projectPath = project.getStr("project_path");
        String dirPath = projectPath + File.separator + projectName + File.separator + "ShareFile";
        String projectId = project.getStr("id");

        Document document = XmlUtil.createXml();
        Element element = getShareConfigXML(document,projectId);
        document.appendChild(element);


        //创建文件夹
        File directory = new File(dirPath);
        if (!directory.exists()) {
            if (!directory.mkdirs()) {

            }
        }
        XmlUtil.toFile(document,dirPath + File.separator + projectName + "_Prj_ShareConfig.xml");
    }
    Element getShareConfigXML(Document document,String projectId){
        Element element = document.createElement("ShareFile");
        element.setAttribute("version","ADE3.3.4.20230522_Release");
        element.setAttribute("NodeEnable","true");
        element.setAttribute("EXISTQUOTE","true");
        String sqlStr = "select b.id as tempalteId,b.template_code as templateType,b.template_name as templateName,a.config_code as configCode from c_project_config a LEFT JOIN c_template b on a.tempalte_id=b.id where  a.config_type = '"+ ProjectConfigTypeEnum.SHARE.getCode()+"' ORDER BY a.sort_num desc";
        List<JSONObject> jsonObjectList = this.sqlExtService.findListObject(sqlStr,JSONObject.class);
        for(int i = 0;i < jsonObjectList.size(); i++) {
            JSONObject itemJSONObject = jsonObjectList.get(i);
            element.appendChild(getShareAdapterDom(document,itemJSONObject,i));
        }
        return element;
    }

    Element getShareAdapterDom(Document document,JSONObject jsonObject,int index){
        Element element = document.createElement("ShareAdapter");
        element.setAttribute("TemplateType",jsonObject.getStr("templateType"));
        element.setAttribute("Index",String.valueOf(index));
        element.setAttribute("Uuid",jsonObject.getStr("configCode"));
        element.setAttribute("NodeEnable","true");
        element.setAttribute("EXISTQUOTE","true");
        String sqlStr = "select attr_code as attrCode,attr_content as attrContent from c_template_attr where template_id = '"+jsonObject.getStr("tempalteId")+"' order by sort_num asc";
        List<JSONObject> jsonObjectList = this.sqlExtService.findListObject(sqlStr,JSONObject.class);
        jsonObjectList.forEach(attrJSONObject -> {
            Element element1 = document.createElement(attrJSONObject.getStr("attrCode"));
            element1.setTextContent(attrJSONObject.getStr("attrContent"));
            element.appendChild(element1);
        });
        return element;
    }
}
